package com.yimils.core.goods.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yimils.core.goods.dto.VideoAddReq;
import com.yimils.core.goods.dto.VideoUpdateReq;
import com.yimils.core.goods.entity.VideoEntity;
import com.yimils.core.goods.mapper.VideoMapper;
import com.yimils.core.goods.serivce.VideoService;
import com.yimils.core.goods.vo.VideoRes;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service("videoService")
public class VideoServiceImpl extends ServiceImpl<VideoMapper, VideoEntity> implements VideoService {
    @Override
    public Boolean saveVideo(VideoAddReq videoAddReq) {
        VideoEntity videoEntity = new VideoEntity();
        videoEntity.setUrl(videoAddReq.getUrl());
        videoEntity.setState(Boolean.FALSE);
        videoEntity.setCreateTime(LocalDateTime.now());
        videoEntity.setUpdateTime(LocalDateTime.now());
        return save(videoEntity);
    }

    @Override
    public List<VideoEntity> saveVideoList(List<String> videoUrls) {
        List<VideoEntity> collect = videoUrls.stream().map(o -> {
            VideoEntity videoEntity = new VideoEntity();
            videoEntity.setUrl(o);
            videoEntity.setState(Boolean.FALSE);
            videoEntity.setCreateTime(LocalDateTime.now());
            videoEntity.setUpdateTime(LocalDateTime.now());
            return videoEntity;
        }).collect(Collectors.toList());
        saveOrUpdateBatch(collect);
        return collect;
    }

    @Override
    public Boolean updateVideo(VideoUpdateReq videoUpdateReq) {
        QueryWrapper<VideoEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(VideoEntity::getId, videoUpdateReq.getVideoId()).eq(VideoEntity::getState, Boolean.FALSE);
        VideoEntity entity = getOne(qw);
        entity.setUrl(videoUpdateReq.getUrl());
        entity.setUpdateTime(LocalDateTime.now());
        return saveOrUpdate(entity);
    }

    @Override
    public Boolean deleteVideo(List<Long> videoIds) {
        QueryWrapper<VideoEntity> qw = new QueryWrapper<>();
        qw.lambda().in(VideoEntity::getId, videoIds).eq(VideoEntity::getState, Boolean.FALSE);
        List<VideoEntity> entities = list(qw);
        entities.forEach(o -> {
            o.setState(Boolean.TRUE);
            o.setUpdateTime(LocalDateTime.now());
        });
        return saveOrUpdateBatch(entities);
    }

    @Override
    public List<VideoRes> listVideo() {
        QueryWrapper<VideoEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(VideoEntity::getState, Boolean.FALSE);
        List<VideoEntity> entities = list(qw);
        return entities.stream().map(o -> {
            VideoRes videoRes = new VideoRes();
            videoRes.setUrl(o.getUrl());
            videoRes.setVideoId(o.getId());
            return videoRes;
        }).collect(Collectors.toList());
    }

    @Override
    public List<VideoEntity> listVideo(List<Long> videoIds) {
        QueryWrapper<VideoEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(VideoEntity::getId, videoIds).eq(VideoEntity::getState, Boolean.FALSE);
        return list(qw);

    }
}
